package com.platform.api;

import com.platform.annotation.LoginUser;
import com.platform.entity.UserVo;
import com.platform.service.ApiUserExchangeService;
import com.platform.util.ApiBaseAction;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "兑换码兑换")
@RestController
@RequestMapping("/api/userExchange")
public class ApiUserExchangeController extends ApiBaseAction {
    @Autowired
    private ApiUserExchangeService apiUserExchangeService;
    /**
     * 兑换码兑换生成订单
     */
    @ApiOperation(value = "订单提交")
    @PostMapping("submit")
    public Object submit(@LoginUser UserVo loginUser) {
        Map resultObj = null;
        try {
            resultObj = apiUserExchangeService.queryObject(getJsonRequest(), loginUser);
            if (null != resultObj) {
                return toResponsObject(MapUtils.getInteger(resultObj, "errno"), MapUtils.getString(resultObj, "errmsg"), resultObj.get("data"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toResponsFail("提交失败");
    }

    /*
    * 验证兑换码和卡号
    * */
    @ApiOperation(value = "验证兑换码和卡号")
    @PostMapping("verify")
    public Object verify(@LoginUser UserVo loginUser) {
        Map resultObj = null;
        try {
            resultObj = apiUserExchangeService.verify(getJsonRequest());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toResponsObject(MapUtils.getInteger(resultObj, "errno"), MapUtils.getString(resultObj, "errmsg"), resultObj.get("data"));
    }
}
